<?php

namespace app\controller\front;

use app\BaseController;
use app\lib\exception\NotFoundException;
use app\lib\exception\VerifyException;
use think\facade\Config;
use think\facade\Cache;
use app\model\Candidate as CandidateModel;
use app\model\Character as CharacterModel;
use app\model\CandidateComment as CandidateCommentModel;
use app\lib\JwtHander;
use app\model\DingGroup as DingGroupModel;
use app\model\DingUser as DingUserModel;
use app\service\DingNotice as DingNoticeService;

class Candidate extends BaseController
{


    public function login()
    {

        $params = request()->param();
        $mobile = $params['mobile'];
        $realname = $params['realname'];

        $candidateModel = new CandidateModel;
        $info = $candidateModel->where(['mobile' => $mobile,'realname' => $realname])->find();

        if(empty($info)){
            throw new VerifyException();
        }

        $jwt = JwtHander::getInstance()->generateJwt($info['id'], request()->host());
        $info = CandidateModel::find($info['id']);
        $jwt['status'] = $info['status'];
        return $this->successMessage('登陆成功',$jwt);        

    }


    public function goDing(){

        $params = request()->param();
        
        $id = JwtHander::getInstance()->decodeJwt()['sub'];
     
        $candidateModel = new CandidateModel();
        $res = $candidateModel->find($id);

        $ding_params = [
            "title" =>  'HR Online消息通知',
            "text" => "您好，有一名候选人".$res['realname']."已经到达公司前台，需要您去负责面试。",
        ];

        $dingUserModel = new DingUserModel();
        $dingNoticeService = new DingNoticeService;
        $user = $dingUserModel->where(['name' => $res['hr']])->find();

        if($user){
            $dingNoticeService->companyMsg($user['id'],'markdown',$ding_params);
        }

        return $this->successMessage('发送成功');

    }


    public function submitComment()
    {

        $params = request()->param();
        
        $id = JwtHander::getInstance()->decodeJwt()['sub'];

        $candidateModel = new CandidateModel();
        $res = $candidateModel->find($id);

        $data = [
        	'candidate_id' => $res['id'],
            'candidate_name' => $res['realname'],
            'hr_name' => $res['hr'],
            'interview_name' => $res['group_interview'],
			'hr_star' => $params['hr_star'],
			'interview_star' => $params['interview_star'],
            'content' => $params['content'],
        ];
        
        $candidateCommentModel = new CandidateCommentModel();
        $candidateCommentModel->save($data);

        return $this->successMessage('评论成功');

    }

    
    
    // 性格测试接口(共40题,四种性格DISC) 
    public function character() {
        // 获取用户token
        $id = JwtHander::getInstance()->decodeJwt()['sub'];
        // 面试人员列表 数据模型
        $candidateModel = new CandidateModel();
        $res = $candidateModel->find($id);
        $params = request()->param();
        // 更新 性格测试表
        $characterModel = new CharacterModel();
        $submitData = $params;
        $flag = $characterModel->where(['user_id' => $res['id']])->count();
        if($flag > 0) {
            $characterModel->update($submitData, ['user_id' => $res['id']]);
        }else {
            $submitData['user_id'] = $res['id'];
            $characterModel->save($submitData);
        }
        
        $res_character = calcDISC($params);

        // 人才测评结果  钉钉推给面试人  附带类型介绍
        /*
        支配型/控制者 d
        活泼型/社交者 i
        稳定型/支持者 s
        完美型/服从者 c
        */

        $resMap = [
            'd' => 'D 支配型/控制者',
            'i' => 'I 活泼型/社交者',
            's' => 'S 稳定型/支持者',
            'c' => 'C 完美型/服从者',
        ];

        $resText = '';
        foreach ($res_character as $v) {
            $resText = $resText.$resMap[$v].'; ';
        }

         $ding_params = [
             "title" =>  'HR Online消息通知',
             "text" => "面试者：".$res['realname']."，面试岗位：".$res['worker']."，已完成性格测评。结果：【".strtoupper(implode(', ',$res_character))."】 解析：".$resText
         ];
        //             "text" => $res['realname']."已完成性格测评，[D-".$params["d"]."，I-".$params["i"]."，S-".$params["s"]."，C-".$params["c"]."]。性格介绍：D支配型/控制者；I:活泼型/社交者；S:稳定型/支持者；C:完美型/服从者；",

         $dingUserModel = new DingUserModel();
         $dingNoticeService = new DingNoticeService;
         $user = $dingUserModel->where(['name' => $res['hr']])->find();
        $group = $dingUserModel->where(['name' => $res['group_interview']])->find();

         if($user){
             // 发给HR
             $dingNoticeService->companyMsg($user['id'],'markdown',$ding_params);
             // 发给面试官
             $dingNoticeService->companyMsg($group['id'],'markdown',$ding_params);
         }

//         $rr = "面试者：".$res['realname']."，面试岗位：".$res['worker']."，已完成性格测评。结果：【".strtoupper(implode(', ',$res_character))."】 解析：".$resText;

        return $this->successMessage('测试成功', $res_character);
    }

    // 获取用户是否测试&测试结果
    public function getCharacter()
    {
        // 获取用户token
        $id = JwtHander::getInstance()->decodeJwt()['sub'];
        $candidateModel = new CandidateModel();
        $user = $candidateModel->find($id);

        $characterModel = new CharacterModel();
        $res = $characterModel->where(['user_id' => $user['id']])->find();
        if($res) {
            $data = [
                'd' => $res['d'],
                'i' => $res['i'],
                's' => $res['s'],
                'c' => $res['c'],
            ];
            $res = calcDISC($data);
        }else {
            return $this->successMessage('没有测试',[],200);
        }
        return $this->successMessage('查询成功',$res);

    }


    public function getCandidateInfo()
    {

        $id = JwtHander::getInstance()->decodeJwt()['sub'];

        $params = request()->param();
        $candidateModel = new CandidateModel();
        $res = $candidateModel->find($id);

        $candidateCommentModel = new CandidateCommentModel();
        $comment = $candidateCommentModel->where(['candidate_id' => $res['id']])->find();
        $res['comment'] = $comment;
        $res['group_name'] = DingGroupModel::find($res['group'])['name'];
        $dingUserModel = new DingUserModel;
        if($res['status'] == 5){
            $res['uid'] = $dingUserModel->where(['name' => $res['realname']])->find()['id'];
        }

        return $this->successMessage('查询成功',$res);  
    }

}